<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- el-tree -->
        <el-tree :data="list">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doEdit(data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="doDel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- 弹框
            解决多次切换添加和编辑，对话框显示数据不一致的问题
            1.使用 v-if="showDialog" 在关闭弹框时，销毁组件，打开时创建组件 ->工作中经常使用
            2.在this.$nextTick中使用ref操控子组件清空表单 ->优化时使用
            3.在子组件中使用watch，侦听器 ->不推荐使用，麻烦
             -->
        <el-dialog :title="isEdit? '编辑':'新增'" :visible.sync="showDialog" @close="hClose">
          <Dialog
            :id="id"
            ref="myDialog"
            :is-edit="isEdit"
            :origin-list="originList"
            @success="Success"
            @close="showDialog = false"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { delDepartment, getDepartments } from '@/api/department'
import { departmentTree } from '@/utils/index'
import Dialog from './dialog.vue'
export default {
  components: {
    Dialog
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      id: '',
      isEdit: false, // 默认为添加，true为编辑
      originList: []
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    hClose() {
      this.$refs.myDialog.resetForm()
    },
    // 获取组织结构
    async getDepartment() {
      const { data } = await getDepartments()
      console.log(data)
      this.originList = data.data.depts.map(item => {
        const { name, code, pid, id } = item
        return { name, code, pid, id }
      })
      data.data.depts.shift() // 删除数组中第一条数据----公司名称
      this.list = departmentTree(data.data.depts)
    },
    // 添加
    doAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.id = id
    },
    // 功能完成
    Success() {
      this.showDialog = false
      this.getDepartment()
    },
    // 编辑
    doEdit(id) {
      this.showDialog = true
      this.id = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.myDialog.getDate()
      })
    },
    // 删除
    doDel(id) {
      this.$confirm('确定要删除此部门吗？').then(async() => {
        const { data } = await delDepartment(id)
        console.log(data)
        this.$message.success(data.message)
        this.getDepartment()
      })
    }
  }
}
</script>
